<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/bootstrap.css">
    <title>登录</title>
</head>

<body>
    <div class="container">
        <h1>免费登录</h1>
        <div class="form">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" class="form-control" id="username" aria-describedby="emailHelp">
                <div class="invalid-feedback">用户名或密码错误</div>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" class="form-control" id="password">
                <div class="invalid-feedback">用户名或密码错误</div>
            </div>
            <div class="form-group form-check">
                <input type="checkbox" class="form-check-input" id="mdl">
                <label class="form-check-label" for="mdl">记住密码</label>
            </div>
            <button type="submit" class="btn btn-success">登录</button>

        </div>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded',function(){
            let username = document.querySelector('#username');
            let password = document.querySelector('#password');
            let mdl = document.querySelector('#mdl');
            let btn = document.querySelector('.btn');


            btn.onclick = function(){
                let _username = username.value.trim();
                let _password = password.value.trim();
                let _mdl = mdl.checked;


                let xhr = new XMLHttpRequest();
                xhr.onload = function(){
                    let data = JSON.parse(xhr.responseText);
                    if(data.code === 200){
                        username.classList.remove('is-invalid');
                        password.classList.remove('is-invalid');
                        location.href = '/mine.html'
                    }else if(data.code === 400){
                        username.classList.add('is-invalid');
                        password.classList.add('is-invalid');
                    }
                }
                xhr.open('get',`/api/user/login?username=${_username}&password=${_password}`,true);
                xhr.send()
            }

           
        })
    </script>
</body>

</html>